//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    // 轮播图
    lunbo:[],
    // 宫格
    list:[],
    // 热卖景点
    rList:[],
    url: app.globalData.imgurl,
    ticketId:"847052840a6246e899ebe914be3b6d10",
    //商品搜索关键字
    lingquyouhui: true,
    key:"",
    memberInfo:wx.getStorageInfoSync("memberInfo")
  },
  
  list:function(e){
    var index = e.currentTarget.id
    var id = e.currentTarget.dataset.aa
    console.log(e.currentTarget.dataset.aa)
  
    if(index == 2){
        wx.navigateTo({
          url: '../snap_up/snap_up',
        })
    }else if(index ==0){
      wx.navigateTo({
        url: '../commodity_list/commodity_list?type=1&id='+id,
      })
    }
    else if (index == 1) {
      wx.navigateTo({
        url: '../commodity_list/commodity_list?type=1&id='+id,
      })
    }
    else if (index == 3) {
      // wx.navigateTo({
      //   url: '../my_about/my_about',
      // })
      wx.navigateTo({
        url: '../commodity_list/commodity_list?type=1&id='+id,
      })
      
    }
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  // 详情
  detail:function(e){
    var that=this;
    var ticketId = e.currentTarget.dataset.id
    // console.log(ticketId)
    wx.navigateTo({
      url: '../list_detail/list_detail?ticketId=' + ticketId,
    })
  },

  onLoad: function (options) {
    console.log(options)
    // 小程序跳转
    if(options.id){
      wx.request({
        url: app.globalData.appletUrl +"/applet/upgradeMember",
        data:{
          parentId:options.id,
          memberId: wx.getStorageSync("memberInfo").id
        },
        success(res){
          console.log(res)
        }
      })
    }

    var that = this
    var that=this
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  
  },
  // 请求数据轮播图和宫格
  shuju: function () {
    var that = this;
    wx.request({
      url: app.globalData.appletUrl + '/applet/indexData',
      data: {
        shopId: app.globalData.shopId
      },
      success: function (res) {
        that.setData({
          lunbo: res.data.data.slideShowList,
          list: res.data.data.prefectureList
        })
      }
    })
  },
  // 热卖景点
  shuju1: function (page) {
    if(page==""||page==null){
      page=1
    }
    var that = this;
    var key=that.data.key
    wx.request({
      url: app.globalData.appletUrl + '/applet/hotTicket',
      data: {
        shopId: app.globalData.shopId,
        page: page,
        key:key,
      },
      success: function (res) {
        var list = that.data.rList.concat(res.data.data)
        console.log(list)
        that.setData({
          rList:list
        })
      }
    })
  },
  // 搜索
  searchticket:function(e){
    var key = e.detail.value
    //  console.log(key)
     var that=this
     that.setData({
       key: key
     })
    this.shuju1()
  },
  // 上拉加载
  // onReachBottom:function(){
  //   var page=1
  //   page++
  //  this.shuju1(page)
  // },
  // 显示
  onShow:function(){
    
    this.shuju();
    this.shuju1();
    this.upMemberInfo();
  },
  getUserInfo: function(e) {
     console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  // 优惠券关闭
  youguanbi: function () {
    this.setData({
      lingquyouhui: true
    })

  },
  //更新用户信息
  upMemberInfo:function(){

    var that=this;
    wx.request({
      url: app.globalData.appletUrl + '/applet/getMemberInfo',
      data: {
        unionId: wx.getStorageSync("openId"),
      },
      success: function (res) {
         
        console.log(res.data);
        if(res.data.code=='000000'){//找到对应的用户信息

          that.setData({
              memberInfo:res.data.data
            });
            


            wx.setStorageSync('memberInfo', res.data.data);
        }

        
      }
    })
      


  },

  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function (res) {
    var that=this
    // console.log(wx.getStorageSync("memberInfo").id)
    // console.log(res)
    if (res.from == 'menu') {

      var member = wx.getStorageSync("memberId")
      wx.request({
        url: app.globalData.appletUrl + '/applet/memberShare',
        data: {
          shopId: app.globalData.shopId,
          memberId: member
        },
        header: {
          "content-type": "application/json"
        },
        success: function (res) {

        }
      })
      wx.request({
        url: app.globalData.appletUrl + '/applet/forwardCoupon',
        data: {
          shopId: app.globalData.shopId,
          memberId: wx.getStorageSync('memberInfo').id
        },
        header: {
          "content-type": "application/json"
        },
        success: function (res) {
          console.log("1212121212121212121212121212121212")
          if (res.data.result) {
            that.setData({
              lingquyouhui: false
            })
          if (res.data.result){

          }
        }
      }
    })



    }
    return {
      title: "我负责省钱，你负责点我",
      path: '/pages/index/index?id=' + wx.getStorageSync("memberInfo").id,
      success:function(res){
        console.log(res)
      }
    }
    

  }
})
